hovercal Visual tests¶

In [1]:
# Whether to output the plots and markdowns for readme
save_plots = True
save_markdowns = False
In [2]:
import pandas as pd
import numpy as np

import hovercal

import datetime

Generated Data Example¶

In [3]:
# Let's say we eat a lot of fruit every day
# Create a df with type of fruit (should be unique in the Fruit_type col, so hovertools can work)
fruit_df = pd.DataFrame({
    'date' : [np.random.choice(pd.date_range(datetime.datetime(2020,1,1),datetime.datetime(2022,1,3))) for i in range(24)],
    'Fruit_servings' : np.random.randint(1,5, 24),
    'Fruit_type' : [str(list(set(np.random.choice(['Strawb', 'Papaya', 'Grapefruit', 'Bloob'], size= np.random.randint(1,5))))) for i in range(24)],
})

# Sometimes we get date duplicates. This will throw an error in plotting.
fruit_df = fruit_df.groupby(['date'], as_index=False).agg({'date': 'first',
                                                            'Fruit_servings': 'sum',
                                                            'Fruit_type': lambda x: str(set(list(x)))})

# Use df_prepper to make day, month, year columns
fruit_df_prepped = hovercal.df_prepper(fruit_df)
# Check it
fruit_df_prepped.head()
Out[3]:
date Fruit_servings Fruit_type year month day
0 2020-01-02 4 {"['Papaya', 'Strawb']"} 2020 1 2
1 2020-01-25 3 {"['Grapefruit']"} 2020 1 25
2 2020-03-19 2 {"['Papaya', 'Strawb', 'Bloob']"} 2020 3 19
3 2020-04-25 1 {"['Papaya', 'Bloob']"} 2020 4 25
4 2020-05-29 3 {"['Papaya', 'Strawb', 'Bloob']"} 2020 5 29
In [4]:
if save_markdowns:
    print(fruit_df_prepped.head().to_markdown())
In [5]:
# Create a list of what columns we want to show up when we hover
# This is useful if you have a dataframe with a lot of excess columns, but
# Only want some of them to show up.
hov = ['Fruit_type']

# Call year_heatmap with all the customizations
fruit_panel_1 = hovercal.year_heatmap(fruit_df_prepped,
             year_list = [2020, 2021],
             fig_height = 160,
             show_toolbar=False,
             hover_columns = hov,
             value_column = 'Fruit_servings',
             empty_color = '#FAFAFA',
             month_separation_color = 'lightblue',
             month_separation_alpha = 0.3,
             outline_color = 'lightblue',
             box_separation_width = 2)

fruit_panel_1
Out[5]:
In [6]:
if save_plots:
    fruit_panel_1.save('../examples/fruit_hovercal_1.png')
    fruit_panel_1.save('../examples/fruit_hovercal_1.html')
In [7]:
fruit_panel_2 = hovercal.year_heatmap(fruit_df_prepped,
             year_list = [2021],
             fig_height = 160,
             show_toolbar=False,
             hover_columns = hov,
             cmap_color = 'RdPu',
             value_column = 'Fruit_servings',
             empty_color = 'white',
             month_separation_color = 'white',
             outline_color = 'purple',
             box_separation_color = 'black',
             box_separation_width = 5)

fruit_panel_2
Out[7]:
In [8]:
if save_plots:
    fruit_panel_2.save('../examples/fruit_hovercal_2.png')
    fruit_panel_2.save('../examples/fruit_hovercal_2.html')
In [9]:
fruit_panel_3 = hovercal.year_heatmap(fruit_df_prepped,
             year_list = [2021],
             fig_height = 160,
             show_toolbar=True,
             hover_columns = hov,
             cmap_color = 'Purples',
             value_column = 'Fruit_servings',
             empty_color = '#FAFAFA',
             month_separation_color = 'grey',
             month_separation_width = 0,
             day_label = 'Full',
             outline_color = 'purple',
             box_separation_color = 'white',
             box_separation_width = 2)

fruit_panel_3
Out[9]:
In [10]:
if save_plots:
    fruit_panel_3.save('../examples/fruit_hovercal_3.png')
    fruit_panel_3.save('../examples/fruit_hovercal_3.html')

Spotify Data Test¶

In [11]:
spotify_df = pd.read_json('endsong_data.json', lines=False)
spotify_df.head()
Out[11]:
ts username ms_played conn_country user_agent_decrypted master_metadata_track_name master_metadata_album_artist_name master_metadata_album_album_name spotify_track_uri episode_name episode_show_name spotify_episode_uri reason_start reason_end shuffle skipped offline offline_timestamp incognito_mode
0 2021-03-14T15:25:02Z lianamerk 11145 US unknown NaN NaN NaN NaN 94: The Pools of Horus The History of Egypt Podcast spotify:episode:6rOdi0QxbzjoS9Z682OoXS appload fwdbtn False NaN False 1615735489358 False
1 2021-01-20T20:46:36Z lianamerk 0 US unknown NaN NaN NaN NaN 72: The Home Front (Letters to Ahmose) The History of Egypt Podcast spotify:episode:4Gu5MQ7GIXqaIun8k2vIE5 fwdbtn logout False NaN False 1611080493371 False
2 2021-08-25T20:38:44Z lianamerk 1958999 US unknown NaN NaN NaN NaN 124: Amurrites 2, The Crimes of Aziru The History of Egypt Podcast spotify:episode:5O8Bbg7ycDAp7SKSYonmd6 clickrow trackdone False NaN True 1629913981120 False
3 2021-06-28T15:56:04Z lianamerk 1353584 US unknown NaN NaN NaN NaN 116: Adoring Aten The History of Egypt Podcast spotify:episode:4QC0C1xCUd0YuAXYyhQQqE appload unexpected-exit-while-paused False NaN False 1624840328345 False
4 2022-02-27T13:31:37Z lianamerk 991664 SG unknown NaN NaN NaN NaN 158: What Ay Did The History of Egypt Podcast spotify:episode:1Ttt4NrGm6g2SJtVKRNocS trackdone trackdone False NaN False 1645967697862 False
In [12]:
podcast_df = hovercal.spotify_cleaner(spotify_df, 'The History of Egypt Podcast')
podcast_df.head()
Out[12]:
date mPlayed day month year date_time episode_name unique_episodes
0 2020-06-16 78.215850 16 6 2020 2020-06-16 03:19:11 {'Episode 1: The Two Lands', 'Episode 2: Horus... 2
1 2020-06-17 62.465767 17 6 2020 2020-06-17 15:21:51 {'Episode 1: The Two Lands', 'Interlude: Infin... 4
2 2020-06-18 38.288933 18 6 2020 2020-06-18 03:36:57 {'Episode 4: The Sacred Ones'} 1
3 2020-06-20 6.900417 20 6 2020 2020-06-20 18:20:34 {'Episode 4: The Sacred Ones'} 1
4 2020-06-25 7.697117 25 6 2020 2020-06-25 20:36:14 {'Episode 4: The Sacred Ones'} 1
In [13]:
# Create the list of what we want to hover over
hov = ['episode_name', 'unique_episodes']

# The big plot!
pod_panel = hovercal.year_heatmap(podcast_df,
             [2020, 2021, 2022],
             fig_height = 160,
             show_toolbar=False,
             hover_columns = hov,
             value_column = 'mPlayed',
             empty_color = '#FAFAFA',
             month_separation_color = 'green',
             month_separation_alpha = 0.3,
             outline_color = 'green',
             box_separation_width = 2,
             cmap_color = 'Greens',
             day_label = 'Short')

pod_panel
Out[13]:
In [14]:
if save_plots:
    pod_panel.save('../examples/podcast_hovercal.png')
    pod_panel.save('../examples/podcast_hovercal.html', title='LM Spotify Pod Panel')